function getRandomColor() {
	return '#' + (function(h) {
		return new Array(7 - h.length).join("0") + h
	})((Math.random() * 0x1000000 << 0).toString(16))
}

function randomNum(minNum, maxNum) {
	switch (arguments.length) {
		case 1:
			return parseInt(Math.random() * minNum + 1, 10);
			break;
		case 2:
			return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
			break;
		default:
			return 0;
			break;
	}
}

function moveMessage(messages) {
	var content = document.getElementById('content');
	var contentLeft = content.offsetLeft;
	var contentTop = content.offsetTop;
	var contentWidth = content.offsetWidth;
	var contentHeight = content.offsetHeight;
	for (var i = 0; i < messages.length; i++) {
		var random = randomNum(1, 5);

		var lableTop = randomNum(contentTop, contentTop + contentHeight - 25);
		var _lable = document.createElement('div');
		_lable.style.position = 'fixed';
		_lable.style.zIndex = 999;

		_lable.style.right = contentLeft + 'px';
		_lable.style.top = lableTop + 'px';
		_lable.style.opacity = 1;
		_lable.style.color = getRandomColor();
		_lable.style.whiteSpace = 'nowrap';
		_lable.append(messages[i]);
		document.body.appendChild(_lable);
		var startLeft = _lable.offsetLeft;
		var lableTimer = setInterval(function() {
			if (_lable.offsetLeft <= (contentLeft)) {
				document.body.removeChild(_lable);
				clearInterval(lableTimer);
			}
			_lable.style.left = (startLeft--) + 'px';
		}, 10);
	}
}
var message = [];
var timer = setInterval(function() {

	var thisSend = message.splice(0, message.length >= 10 ? 10 : message.length);
	if (thisSend.length > 0) {
		moveMessage(thisSend);
	}

}, 100);




function heartCheck() {
	document.getElementById("heart-line").style.visibility = "visible";
	var timer = setInterval(function() {
		document.getElementById("heart-line").style.visibility = "hidden";
		clearInterval(timer);
	}, 3000);
}

function Toast(msg, duration) {
	duration = isNaN(duration) ? 3000 : duration;
	var m = document.createElement('div');
	m.innerHTML = msg;
	m.style.cssText =
		"max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(255,106,106,.5);font-size: 16px;";
	document.body.appendChild(m);
	setTimeout(function() {
		var d = 0.5;
		m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
		m.style.opacity = '0';
		setTimeout(function() {
			document.body.removeChild(m)
		}, d * 1000);
	}, duration);
}
var socket;
if (typeof(WebSocket) == "undefined") {
	Toast("不支持的浏览器", 2000);
} else {
	socket = new WebSocket("wss://www.meetion.ren/x-chat/server/admin");
	//socket = new WebSocket("ws://localhost:37201/x-chat/server/admin");
	socket.onopen = function() {
		console.log("连接成功");
		var timer = setInterval(function() {
			try {
				socket.send('HEARTCHECK');
			} catch (e) {
				clearInterval(timer);
			}
		}, 10000);
	};
	socket.onmessage = function(msg) {
		if (msg.data == 'HEARTCHECK') {
			heartCheck();
			return;
		}
		message.push(msg.data.trim());
		Toast(msg.data, 2000);
	};
	socket.onclose = function() {
		socket = new WebSocket("wss://www.meetion.ren/x-chat/server/admin");
		//socket = new WebSocket("ws://localhost:37201/x-chat/server/admin");
	};
	socket.onerror = function() {
		socket = new WebSocket("ws://localhost:37201/x-chat/server/admin");
		socket = new WebSocket("wss://www.meetion.ren/x-chat/server/admin");
	}
}
var chat_iput = document.getElementById('chat_iput');
var input = document.getElementById('input');

var height = chat_iput.offsetHeight;
chat_iput.style.top = -(70) + 'px';

function getX(e) {
	e = e || window.event;
	return e.pageX || e.clientX + document.body.scroolLeft;
}

function getY(e) {
	e = e || window.event;
	return e.pageY || e.clientY + document.body.scrollTop;
}
document.onclick = function(e) {
	var top = chat_iput.offsetTop;
	var left = chat_iput.offsetLeft;
	var height = chat_iput.scrollHeight;
	var width = chat_iput.scrollWidth;
	var mouseX = getX(e);
	var mouseY = getY(e);
	if (mouseX > left && mouseX < left + width && mouseY > top && mouseY < top + height) {
		return;
	} else {
		close();
	}
};

function show() {
	if (chat_iput.offsetTop >= 10) {
		return;
	}
	var timer = setInterval(function() {
		if (chat_iput.offsetTop >= 10) {
			clearTimeout(timer);
		}
		chat_iput.style.top = chat_iput.offsetTop + 1 + 'px';
	}, 1);
	input.disabled = false;
	input.focus();
}

function close() {
	var height = chat_iput.offsetHeight;
	if (chat_iput.offsetTop <= -(height - 30)) {
		return;
	}
	var timer = setInterval(function() {
		if (chat_iput.offsetTop <= -(height - 30)) {
			clearTimeout(timer);
		}
		chat_iput.style.top = chat_iput.offsetTop - 1 + 'px';
	}, 10);
	input.disabled = true;
}

function submit(e) {
	var evt = window.event || e;
	if (evt.keyCode == 13) {
		socket.send(input.value.trim());
		input.value = "";
	}
}
